﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CCWin;
using System.Threading;

namespace 易保全
{
    public partial class ProcessBarForm : CCSkinMain
    {
        private int mode;//进度条增度模式 {1,2},根据构造函数确定
        private int maxTime;
        private int minTime;
        private int maxRandom;
        private int minRandom;
        /// <summary>
        /// 进度条窗口
        /// </summary>
        /// <param name="minTime">最短显示秒数</param>
        /// <param name="maxTime">最长显示秒数</param>
        /// <param name="title">窗口标题</param>
        public ProcessBarForm(int minTime, int maxTime, string title)
        {
            InitializeComponent();
            this.mode = 1;
            this.Text = title;
            this.delay = 10;
            this.timer1.Interval = 1000;
            this.timer1.Enabled = true;
            this.maxTime = maxTime;//最长显示时间
            this.minTime = minTime;//最短显示时间
            this.maxRandom = prcBar.Maximum % minTime == 0 ? prcBar.Maximum / minTime : prcBar.Maximum / minTime;//每次随机产生数字的最大值
            this.minRandom = prcBar.Maximum % maxTime == 0 ? prcBar.Maximum / maxTime : prcBar.Maximum / maxTime + 1;//每次随机产生数字的最小值
        }
        private long delay = 0;//总延时秒数
        private long delayRemain = 0;//延时还剩秒数
        /// <summary>
        /// 进度条窗口
        /// </summary>
        /// <param name="delaySeconds">延时秒数</param>
        /// <param name="title">窗口标题</param>
        public ProcessBarForm(long delaySeconds,string title)
        {
            InitializeComponent();
            this.mode = 2;
            this.Text = title;
            this.delay = delaySeconds;
            this.delayRemain = delaySeconds;
            this.timer1.Interval = 1000;
            this.timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random random = new Random();
            int addValue=0;
            if (mode == 1)
            {
                addValue = random.Next(minRandom, maxRandom);
                if (prcBar.Value >= prcBar.Maximum)
                {
                    this.timer1.Enabled = false;
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    if (prcBar.Maximum - prcBar.Value > addValue)
                        prcBar.Value += addValue;
                    else
                        prcBar.Value = prcBar.Maximum;
                }
            }
            else
            {
                delayRemain = delayRemain - 1;
                if (prcBar.Value >= prcBar.Maximum)
                {
                    this.timer1.Enabled = false;
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    addValue = (delayRemain == 0) ? prcBar.Maximum - prcBar.Value : random.Next(1, prcBar.Maximum - prcBar.Value);
                    prcBar.Value = (prcBar.Maximum - prcBar.Value > addValue) ? prcBar.Value + addValue : prcBar.Maximum;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
